/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : FillBlanksHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Dec 1, 2008       Raghu Sarraff			       Created
 */
package lwf.handler.english;

import java.util.Arrays;
import java.util.List;

import net.lwf.entity.AttemptedQuestion;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.number.util.RandomNumberGenerator;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;

import lwf.handler.IProcessHandler;
import lwf.handler.ProcessHandler;
import lwf.handler.helper.DisplayFormatter;
import lwf.handler.helper.FormatAttribute;


/**
 * Type the class description here
 *  
 */
public class FillBlanksHandler extends ProcessHandler implements IProcessHandler {

    private String[] data = null; 
    private String type = null;
    private FormatAttribute format;
    
    public FillBlanksHandler(String _type) {
    	this.type = _type;
    	this.init();
    }
    
	/**
	 * @see net.lwf.handler.IProcessHandler#getDisplayFormatter()
	 */
	public DisplayFormatter getDisplayFormatter() {
		format = new FormatAttribute();
		
		format.setQuestionNumber(getQuestionNum());
		format.setQuestionTable("Fill the missing " + this.type, this.generateQuestion());
		format.setQuestionType(4); 
		
        format.setAddOnClickForOK(true);
		
		return new DisplayFormatter(format);
	}
    
    /**
	 * @see net.lwf.handler.IProcessHandler#getQuestionDesc()
	 */
	public String getQuestionDesc() {
		String qDesc = "What is missing in the following? <br>";
		for(int i=0; i<data.length; i++) {
			qDesc += data[i];
		}
		return qDesc;
	}

    private void init() {
    	if(this.type.equals(IHandlerConstant.AlPHABET_TYPE)) {
    		initAlphabetFills();	
    	}
    	else if(this.type.equals(IHandlerConstant.NUMBER_TYPE)) {
    		initNumberFills();	
    	}
    }
    
    /**
	 * 
	 */
	private void initAlphabetFills() {
		String[] alphabets = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
		int caserequired = RandomNumberGenerator.generate(1, 2);
    	if(caserequired == 2) {
    		//All upper case letters
    		for(int i=0; i<alphabets.length; i++) {
    			alphabets[i] = alphabets[i].toUpperCase();
    		}
    	}
    	int requiredBlanks = RandomNumberGenerator.generate(1, 15);
    	int[] questions = RandomNumberGenerator.generateInts(0, 25, requiredBlanks);
    	Arrays.sort(questions);
    	String answer = "";
    	//String[] answers = new String[requiredBlanks];
    	for(int i=0; i<questions.length; i++) {
    		int pos = questions[i];
    		//answers[i] = alphabets[pos];
    		answer += alphabets[pos];
    		if(i+1<questions.length) {
    			answer += ",";
    		}
    		alphabets[pos] = "?";
    	}
    	setAnswer(answer);
    	data = alphabets;
    	
	}
	
	private void initNumberFills() {
		int[] numbs = RandomNumberGenerator.generateInts(1, 25, 25);
		Arrays.sort(numbs);
		String[] numbers = new String[numbs.length];
		for(int i=0; i<numbers.length; i++) {
			numbers[i] = String.valueOf(numbs[i]);
		}
    	int requiredBlanks = RandomNumberGenerator.generate(1, 15);
    	int[] questions = RandomNumberGenerator.generateInts(1, 24, requiredBlanks);
    	Arrays.sort(questions);
    	String answer = "";
    	for(int i=0; i<questions.length; i++) {
    		int pos = questions[i];
    		answer += numbers[pos];
    		if(i+1<questions.length) {
    			answer += ",";
    		}
    		numbers[pos] = "?";
    	}
    	setAnswer(answer);
    	data = numbers;
	} //initFillNumbers

	private String generateQuestion() {
		StringBuffer sbQBlock = new StringBuffer();

		sbQBlock.append("<table  width=\"80%\" border=\"0\" align=\"center\" cellspacing=\"0\" cellpadding=\"0\">");
		boolean countdisplay = false;
		int nCounter = 0;
		int nDataCount = 0;
        while(!countdisplay) {
        	sbQBlock.append("<tr>");
	        	for(int i=0; i<10; i++) {
	        		sbQBlock.append("<td  width=\"5%\" class=\"datacenter\">");
	        		    if(nDataCount<data.length && !countdisplay ) {
							if(data[nDataCount].equals("?")) {
								sbQBlock.append("<input type=\"text\" class=\"smallersize\" name=\"useranswer" + String.valueOf(nDataCount+1) +  "\" size=\"1\"/>");
							}
							else {
								sbQBlock.append(data[nDataCount]);
							}
							nDataCount++;
	        		    }
	        		    else {
	        		    	sbQBlock.append("<img src=\"image/empty.gif\">");
	        		    	countdisplay = true;
	        		    }
	        		sbQBlock.append("</td>");
	        		sbQBlock.append("<td width=\"3%\" >");
	        			sbQBlock.append("<img src=\"image/empty.gif\">");
	        		sbQBlock.append("</td>");
	        	}
	        	nCounter = nCounter + 10;
        	sbQBlock.append("</tr>");
        	sbQBlock.append("<tr>");
        		sbQBlock.append("<td colspan=\"10\"> &nbsp; </td>");
        	sbQBlock.append("</tr>");
        }
        sbQBlock.append("</table>");      			

        return sbQBlock.toString();
    }

	//------------------------------------------------ Methods for PDF generation on the browser
	public void generate(Document document) {
	
	}

	@Override
	public List<QuestionInfo> generateMultiple() {
		return null;
	}

	@Override
	public QuestionInfo generateSingle() {
		return null;
	}

	@Override
	public int getColumns() {
		return 0;
	}

	@Override
	public void getContent(Document document) throws DocumentException {
	}

	@Override
	public int getRows() {
		return 0;
	}

	@Override
	public boolean isPortrait() {
		return false;
	}

	@Override
	public String getPdfInstructionalText() {
		return null;
	}

	public boolean matchAnswer(String useranswer, AttemptedQuestion aq) {
		return false;
	}

	@Override
	public String getPdfNote() {
		return null;
	}
}

